<?php
/**
 * Created by JetBrains PhpStorm.
 * User: zhouyang
 * Date: 13-7-15
 * Time: 下午4:31
 * To change this template use File | Settings | File Templates.
 */
set_time_limit(0);

include 'bootstrap.php';

//note 取得样式文件内容

//$url = 'http://www.jb51.net/css/';
//$data = file_get_contents('http://img.jb51.net/skin/newblue/main.css');
//
//preg_match('/(.*\/\/.*?)\//', $url, $host);
//
//$host = $host[1];
//
//if (!is_dir('img')) {
//    //mkdir('img');
//}
//$regex = '/url\(\'{0,1}\"{0,1}(.*?)\'{0,1}\"{0,1}\)/';
//
//preg_match_all($regex, $data, $result);
//
//foreach ($result[1] as $val) {
//
//    if (preg_match('/^http.*/', $val)) {
//        $target = $val;
//    } else if (preg_match('/^\/.*/', $val)) {
//        $target = $host . $val;
//    } else {
//        $target = $url . $val;
//    }
//
//    echo $target . "<br/>\r\n";
//
//    $path_info = pathinfo($target);
//
//    echo $path_info['dirname'].'<br>';
//
//    preg_match('/.*\/(.*\.\D+)$/', $val, $name);
//
//    if (!is_file('./img/' . $name[1])) {
//        //copy($target, './img/' . $name[1]);
//    }
//
//}
//
//exit;

class Copy extends Action{
    private $url;
    private $file_name;
    private $css_path = 'css';
    private $js_path = 'js';
    private $img_path = 'image';

    private $url_host;

    private $domain;

    private $html;


    public function __construct(){

    }

    private function downCss(){
        $css_arr = array();
        $dir = $this->mkdirs($this->domain . '/' .$this->css_path);

        if($dir){
            $dom = $this->html->find('link');
            foreach($dom as $v){
                if($v->href && strpos($v->href,'.css')){
                    $css_pathinfo = pathinfo($v->href);
                    $css_basename = explode('?',$css_pathinfo['basename']);
                    $css_basename = $css_basename[0];

                    $down_url = strpos($v->href,'http://') === false ?  $this->url_host . $v->href : $v->href;

                    $css_arr[] =  $down_url;
                    $ret = copy($down_url,$this->domain . '/' .$this->css_path . '/' . $css_basename);

                    if(!$ret){
                        echo '抓取CSS文件' . $css_basename . '失败<br>';

                        unset($ret);
                    }
                }
            }
        }else{
            echo '创建目录'.$this->domain . '/' .$this->css_path .'失败<br>';
            exit;
        }

        $this->css_arr = $css_arr;

        if(!empty($css_arr)){
            $this->downImg();
        }
    }

    private function downImg(){
        foreach((array)$this->css_arr as $v){
            $css_img_url = $this->url;

            $data = file_get_contents($v);

            preg_match('/(.*\/\/.*?)\//', $css_img_url, $host);

            $host = $host[1];

            $dir = $this->mkdirs($this->domain . '/' .$this->img_path);

            if($dir){
                $regex = '/url\(\'{0,1}\"{0,1}(.*?)\'{0,1}\"{0,1}\)/';

                preg_match_all($regex, $data, $result);

                foreach ($result[1] as $val) {

                    if (preg_match('/^http.*/', $val)) {
                        $target = $val;
                    } else if (preg_match('/^\/.*/', $val)) {
                        $target = $host . $val;
                    } else {
                        $target = $css_img_url . $val;
                    }

                    //echo $target . "<br/>\r\n";

                    preg_match('/.*\/(.*\.\D+)$/', $val, $name);

                    if (!is_file($this->domain . '/' .$this->img_path . '/' . $name[1])) {
                        $ret = copy($target, $this->domain . '/' .$this->img_path . '/' . $name[1]);

                        if(!$ret){
                            echo '抓取Img文件' . $target . '失败<br>';

                            unset($ret);
                        }
                    }

                }
            }else{
                echo '创建目录'.$this->domain . '/' .$this->img_path .'失败<br>';
                exit;
            }
        }
    }

    public function index(){

        $url = $this->input()->get('url');
        $file_name = $this->input()->get('file_name');

        $this->url = $url;

        //获取抓取站点 domain
        $tmp =  parse_url($url);
        //抓取页面的host
        $this->url_host = 'http://' . $tmp['host'];
        $tmp_host = explode('.',$tmp['host']);
        $this->domain = $tmp_host[1];

        //获取文件名
        $temp = pathinfo($url);
        $this->file_name = $file_name ? $file_name : $temp['basename'];

        //获取页面全部节点
        $this->html = file_get_html($url);

        //执行抓取css
        $this->downCss();
    }

    public function mkdirs($dir,$mode=0777)
    {
        if (is_dir($dir) || @mkdir($dir,$mode,true)){
            chmod($dir,$mode);
            return true;
        }
        if (!mk_dir(dirname($dir),$mode)){
            return false;
        }

        if(@mkdir($dir,$mode,true)){
            chmod($dir,$mode);
            return true;
        }else{
            return @mkdir($dir,$mode,true);
        }
    }
}

if($_GET['url'] && $_GET['css_img_url']){
    //抓取调用
    $copy = new Copy();

    $copy->index();

    echo '<a href="'.SITE_URL.'/index.php">返回<<</a>';
    exit;
}

?>

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8" />
    <title>页面抓取</title>
</head>
<body>
<form action="<?php echo SITE_URL; ?>/index.php" method="get">
    <div style="margin-top: 20px;margin-left: 50px">
        <h3>
             HTML,JS,CSS,IMAGE 抓取页面
        </h3>
        抓取页面地址：<input type="text" name="url" style="width:280px"><br>
        样式图片地址：<input type="text" name="css_img_url" style="width:280px"><br>
        <br>
        <input type="submit" name="ok" value=" 提 交 ">
    </div>
</form>
</body>
</html>